import wx

from serve import func


class ExitDialog(wx.Dialog):
    """自定义退出对话框，用户可以自己选择退出应用还是在任务托盘中继续运行"""
    def __init__(self, frame, parent=None):
        super(ExitDialog, self).__init__(parent=parent)
        self.frame = frame
        self.init_ui()

    def init_ui(self):
        pnl = wx.Panel(self)
        box_sizer = wx.BoxSizer(wx.VERTICAL)

        rb_sizer = wx.BoxSizer(wx.HORIZONTAL)
        self.rb_hidden = wx.RadioButton(parent=pnl, label="隐藏到任务托盘", style=wx.RB_GROUP)
        self.rb_exit = wx.RadioButton(parent=pnl, label="退出程序")
        rb_sizer.Add((0, 0), 1, wx.EXPAND, 5)   # 弹簧，将控件强行挤到中间去
        rb_sizer.Add(self.rb_hidden, flag=wx.ALL | wx.ALIGN_BOTTOM, border=20)
        rb_sizer.Add(self.rb_exit, flag=wx.ALL | wx.ALIGN_BOTTOM, border=20)
        rb_sizer.Add((0, 0), 1, wx.EXPAND, 5)

        bt_sizer = wx.BoxSizer(wx.VERTICAL)
        self.bt_verify = wx.Button(parent=pnl, id=wx.ID_YES, label="确认")
        self.bt_verify.Bind(wx.EVT_BUTTON, self.proc_exit)
        bt_sizer.Add(self.bt_verify, flag=wx.ALL | wx.ALIGN_CENTER_HORIZONTAL, border=20)

        box_sizer.Add(rb_sizer, 1, wx.EXPAND, 5)
        box_sizer.Add(bt_sizer, 1, wx.EXPAND, 5)
        pnl.SetSizer(box_sizer)

    def proc_exit(self, event):
        """根据用户选择的退出方式，执行不同方法"""
        if self.rb_exit.GetValue():
            func.force_exit()
        if self.rb_hidden.GetValue():
            self.frame.Hide()
            self.Close(True)
